package cn.flying.system.web.cm;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.system.common.vo.OrgTreeTableInfo;
import cn.flying.system.domain.cm.CmOrganization;
import cn.flying.system.service.cm.CmCodeService;
import cn.flying.system.service.cm.CmOrganizationService;
import cn.flying.base.common.vo.Rt;
import cn.flying.base.common.vo.TreeSelectInfo;
import cn.flying.base.common.vo.TreeTableInfoVo;
import cn.flying.base.common.vo.XmSelect;

/**
 * @description: 组织机构管理
 * @author: lvyong
 * @date: 2022年05月22日 17:58
 * @version: 1.0
 */
@Controller
@RequestMapping("organization")
public class CmOrganizationController {

    @Autowired
    private CmOrganizationService cmOrganizationService;
    @Autowired
    private CmCodeService cmCodeService;

    /**
     * 列表页面
     * @return
     */
    @RequestMapping("list")
    public String list(Model model) {
        Map<String, String> orgTypes = cmCodeService.getCodeList("orgType");
        model.addAttribute("orgTypes", orgTypes);
        Map<String, String> orgLevels = cmCodeService.getCodeList("orgLevel");
        model.addAttribute("orgLevels", orgLevels);
        return "sys/organization/list";
    }

    /**
     * 加载资源treetable
     * @return
     */
    @RequestMapping("getOrganizationTreeTableList")
    @ResponseBody
    public TreeTableInfoVo getOrganizationTreeTableList() {
        List<OrgTreeTableInfo> list = cmOrganizationService.getOrganizationTreeTableList();
        return new TreeTableInfoVo(0, "成功", list.size(), list);
    }

    @RequestMapping("getOrganizationTree")
    @ResponseBody
    public List<TreeSelectInfo> getOrganizationTree() {
        List<TreeSelectInfo> treeList = cmOrganizationService.getOrganizationTree();
        return treeList;
    }

    /**
     * xm-select 组织下拉框数据
     * @return
     */
    @PostMapping(value = "getOrganizationSelectList")
    @ResponseBody
    public Rt<List<XmSelect>> getOrganizationSelectList() {
        List<XmSelect> treeList = cmOrganizationService.getOrganizationSelectList();
        return Rt.success( treeList);
    }

    /**
     * 新增/修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/turn/edit")
    public String turnEdit(Long id, String orgId, Model model) {
        Map<String, String> orgTypes = cmCodeService.getCodeList("orgType");
        model.addAttribute("orgTypes", orgTypes);
        Map<String, String> orgLevels = cmCodeService.getCodeList("orgLevel");
        model.addAttribute("orgLevels", orgLevels);
        if ( id != null) {
            CmOrganization organization = cmOrganizationService.getOrganizationById(id);
            model.addAttribute("organization", organization);
        } else {
            CmOrganization organization = new CmOrganization();
            organization.setParentId(orgId);
            model.addAttribute("organization", organization);
        }
        return "sys/organization/edit";
    }

    /**
     * 保存
     * @param organization
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public Rt save(@RequestBody CmOrganization organization) {
        if (organization.getId() == null) {
            cmOrganizationService.insertOrganization(organization);
        } else {
            cmOrganizationService.updateOrganizationById(organization);
        }

        return Rt.success("保存成功！");
    }

    /**
     * 删除
     * @param organization
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public Rt del(@RequestBody CmOrganization organization) {
        cmOrganizationService.deleteOrganizationById(organization.getId());
        return Rt.success("删除成功！");
    }
}
